package com.xidian.zhiancloud.controller.equipFacility.defendManager;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.equipFacility.DefendPlanMakeDTO;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.DefendPlanItem;
import com.xidian.zhiancloud.model.DefendPlanMake;
import com.xidian.zhiancloud.qo.equipFacility.DefendPlanMakeQO;
import com.xidian.zhiancloud.qo.equipFacility.DefendRegisterQO;
import com.xidian.zhiancloud.service.equipFacility.DefendPlanMakeService;
import com.xidian.zhiancloud.vo.equipFacility.DefendPlanMakeDTOVO;
import com.xidian.zhiancloud.vo.equipFacility.DefendPlanMakeVO;
import com.xidian.zhiancloud.vo.equipFacility.DefendRegisterVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 维护计划制定接口
 * @Author: 任启亮
 * @Date: 2020/11/11
 */
@RestController
@RequestMapping("/DefendPlanMake")
public class DefendPlanMakeController extends BaseController {
    @Autowired
    private DefendPlanMakeService defendPlanMakeService;

    /**
     * 增加维护计划制定
     * @param defendPlanMakeDTO
     * @return
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> add(DefendPlanMakeDTO defendPlanMakeDTO) {
        defendPlanMakeDTO.getDefendPlanMake().setCompanyId(getSessionUserCompanyId());
        defendPlanMakeDTO.getDefendPlanMake().setCreator(getSessionUserid());
        defendPlanMakeDTO.getDefendPlanMake().setCreateTime(new Date(System.currentTimeMillis()));
        defendPlanMakeDTO.getDefendPlanMake().setStatus(0);
        Boolean result = defendPlanMakeService.add(defendPlanMakeDTO);
        return ResultDTO.successOf(result, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id获取维护计划记录
     * @param id 维护计划记录对象的id
     * @return 维护计划记录对象
     */
    @GetMapping("/getById")
    public ResultDTO<DefendPlanMakeVO> getById(@RequestParam("id") Long id) {
        return ResultDTO.successOf(defendPlanMakeService.getById(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id删除维护计划记录
     * @param ids ids数组
     * @return 返回删除标志
     */
    @PostMapping("/deleteByIds")
    public ResultDTO<Boolean> deleteByIds(Long[] ids) {
        Boolean result = defendPlanMakeService.delete(ids);
        return ResultDTO.successOf(result, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id修改维护计划记录
     *
     * @param defendPlanMakeDTO 维护计划对象
     * @return 返回修改状态
     */
    @PostMapping("/patchById")
    public ResultDTO<Boolean> patchDTOById(DefendPlanMakeDTO defendPlanMakeDTO) {
        defendPlanMakeDTO.getDefendPlanMake().setCompanyId(null);
        defendPlanMakeDTO.getDefendPlanMake().setCreator(null);
        defendPlanMakeDTO.getDefendPlanMake().setCreateTime(null);
        defendPlanMakeService.patchById(defendPlanMakeDTO);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * @param defendPlanMakeQO 传递参数
     * @return 返回分页列表
     */
    @GetMapping("/getListByQO")
    public ResultDTO<IPage<DefendPlanMakeVO>> getListByQO(DefendPlanMakeQO defendPlanMakeQO) {
        if (defendPlanMakeQO.getCurrent() == null || defendPlanMakeQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        defendPlanMakeQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(defendPlanMakeService.getListByQO(defendPlanMakeQO), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 发布
     * @param id 维护计划id
     * @return
     */
    @GetMapping("/submit")
    public ResultDTO<Boolean> submit(@RequestParam("id") Long id) {
        return ResultDTO.successOf(defendPlanMakeService.submit(id), GeneralResponseEnums.SUCCESS);
    }
}
